import random
import os
import re
import time


#file = open("ftest.txt",'r')
#lines = file.readlines()
#num = int((lines[-3])[0:1])


def clearmsg():
    f = open('faddman.txt','w+')
    f.write('****jski约人滑雪助手****'+'\n'+'0:小写j指令 j约人+内容(j约人明晚尖峰) j加(j加2) j删+序号(j删4) j显示\n'+'****jski西南公告更新****'+'\n'+'单板教学、免费滑行视频分析@六岁 @天一'+'\n')
    f.close()

def addmsg(s,name):
    file = open("faddman.txt",'r')
    lines = file.readlines()
    timenow = time.strftime('%Y-%m-%d',time.localtime(time.time()))
    num = int((lines[-3])[0:1])
    num = num+2
    delmsg('****jski西南公告更新****')
    delmsg('单板教学、免费滑行视频分析@六岁 @天一')
    f = open('faddman.txt','a')
    f.write(str(num)+':(发布时间'+str(timenow)+')'+s+'@%s '%(name)+'\n')
    f.write('****jski西南公告更新****\n')
    f.write('单板教学、免费滑行视频分析@六岁 @天一\n')
    f.close()

def showmsg():
    with open('faddman.txt', 'r') as f:
    #print(f.read())
        txtmsg = str(f.read())
    f.close()
    return txtmsg

def delmsg(s):
    lines = (i for i in open('faddman.txt','r') if s not in i )
    f = open('faddman_new.txt', 'w', encoding="utf-8")
    f.writelines(lines)
    f.close()
    os.rename('faddman.txt', 'faddman.bak')
    os.rename('faddman_new.txt', 'faddman.txt')
    os.remove('faddman.bak')
    #f = open('faddman.txt', 'r', encoding="utf-8")
    #f.close()

def addnum():
    file = open("faddman.txt",'r')
    lines = file.readlines()
    n = 0
    with open("faddman.txt",'w') as f:
        for line in lines:
            if lines[1] == lines[-3]:
                pass
            elif n in [0,len(lines)-1,len(lines)-2]:
                f.write(line)
                n=n+1
            else:
                newline = line[2:]
                a=(n-1)*2
                f.write(str(a)+':'+newline)
                n=n+1
    f.close()

def delnum(delnum):
    file = open("faddman.txt",'r')
    lines = file.readlines()
    n = 0
    newdelnum = delnum/2+1
    if lines[1] == lines[-4]:
        clearmsg()
    else:
        with open("faddman.txt",'w') as f:
            for line in lines:
                if n in [0,len(lines)-1,len(lines)-2]:
                    f.write(line)
                    n=n+1
                elif n == newdelnum:
                    n = n + 1
                else:
                    a=(n-1)*2
                    newline = line[2:]
                    f.write(str(a)+':'+newline) 
                    n=n+1
            f.close()

def addman(num,name):
    file = open("faddman.txt",'r')
    lines = file.readlines()
    n = 0
    newnum = num/2+1
    with open("faddman.txt",'w') as f:
        for line in lines:
            if n in [0,len(lines)-1,len(lines)-2]:
                f.write(line)
                n=n+1
            elif n == newnum:
                newline = line[2:]
                newline = newline.replace('\n','')
                a=(n-1)*2
                f.write(str(a)+':'+newline+'@%s '%(name)+'\n')
                n = n+1
            else:
                newline = line[2:]
                a=(n-1)*2
                f.write(str(a)+':'+newline)
                n=n+1
        f.close()


def skihelp(act,name):
    file = open("faddman.txt",'r')
    lines = file.readlines()
    num = int((lines[-3])[0:1])
    act = act.replace('\n', '')
    act = act.replace(' ','')
    if 'j约人' in act:
        act = act.replace('j约人','')
        addmsg(act,name)
        #print(showmsg())
    elif 'j删' in act:
        act = act.replace('j删','')
        act=int(act)
        if int(act)==0:
            print('不可删除')
        elif num == act ==2:
            clearmsg()
            print('最后一行被删除')
        elif act>num:
            print('没有这一行')
        else:
            delnum(act)
            addnum()
            #print(showmsg())
    elif 'j加' in act:
        act = act.replace('j加','')
        act=int(act)
        addman(act,name)
        #print(showmsg())
    elif 'j显示' in act:
        pass
        #print(showmsg())
    elif 'j清空' in act:
        clearmsg()
    msgshow = showmsg()
    return msgshow

